﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Huioo.Learning.WPF.WpfApp;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    Point _pos;
    bool _isDrawing;
    Brush _stroke = Brushes.Black;

    Window1 _window1 = new();
    Window2 _window2 = new();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnChangeValue(object sender, RoutedEventArgs e)
    {
        _simple.YearPublished++;
    }

    private void OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        var rect = e.Source as Rectangle;
        if (rect != null)
        {
            _stroke = rect.Fill;
        }
        else
        {
            _isDrawing = true;
            _pos = e.GetPosition(_root);
            _root.CaptureMouse();
        }
    }

    void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (_isDrawing)
        {
            Line line = new Line();
            line.X1 = _pos.X;
            line.Y1 = _pos.Y;
            _pos = e.GetPosition(_root);
            line.X2 = _pos.X;
            line.Y2 = _pos.Y;
            line.Stroke = _stroke;
            line.StrokeThickness = 1;
            _root.Children.Add(line);
        }
    }

    void OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        _isDrawing = false;
        _root.ReleaseMouseCapture();
    }

    private void OnModifyBrush(object sender, RoutedEventArgs e)
    {
        // 在运行时操作资源字典，添加或删除资源
        //this.Resources.Add("brush2", new SolidColorBrush(Color.FromRgb(200, 10, 150)));
        //this.Resources.Remove("brush1");

        // 修改资源，而不是替换
        var brush = (LinearGradientBrush)this.Resources["brush1"];
        brush.GradientStops.Add(new GradientStop(Colors.Blue, .5));
    }

    private void OnReplaceBrush(object sender, RoutedEventArgs e)
    {
        // StaticResource绑定属性保持引用旧对象。
        // DynamicResource 标记扩展将动态绑定到资源，这意味着如果对象本身被新对象（使用相同的密钥）取代，则使用新资源。
        var brush = new RadialGradientBrush();
        brush.GradientStops.Add(new GradientStop(Colors.Blue, 0));
        brush.GradientStops.Add(new GradientStop(Colors.White, 1));
        this.Resources["brush1"] = brush;
    }

    private void OnOpenWindow(object sender, RoutedEventArgs e)
    {
        var btn = e.Source as Button;
        switch ((string)btn.Content)
        {
            case "Window1":
                _window1.ShowDialog();
                break;
            case "Window2":
                _window2.ShowDialog();
                break;
            default:
                break;
        }
        
    }
}